package com.hxzy.controller.admin.system;

import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.entity.DictData;
import com.hxzy.common.core.page.TableDataInfo;
import com.hxzy.common.utils.enums.BusinessType;
import com.hxzy.system.dto.DictDataDTO;
import com.hxzy.system.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 通用字典值查询
 *
 * @author tonneyyy
 */
@RequestMapping(value = "/admin")
@RestController
public class DictDataController extends BaseController {


    @Autowired
    private DictDataService  dictDataService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/dict/data/list")
    public TableDataInfo dataInfo(DictDataDTO dictDataDTO){
        super.startPage();
        List<DictData> list= dictDataService.search(dictDataDTO);
        return super.getDataTable(list);
    }


    /**
     * 根据类型查询值
     * @param dictType
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping(value = "/dict/data/type/{dictType}")
    public AjaxResult selectDataTypeByTypeName( @PathVariable(value = "dictType") String dictType) {
        List<DictData> dictDataList = this.dictDataService.selectDictDataByType(dictType);
        return AjaxResult.success(dictDataList);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/dict/data/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectByPrimaryKey(dictCode));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/dict/data")
    public AjaxResult edit(@Validated @RequestBody DictData dict)
    {
        dict.setUpdateBy(getUsername());
        dict.setUpdateTime(new Date());
        return toAjax(dictDataService.updateByPrimaryKeySelective(dict));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping(value = "/dict/data")
    public AjaxResult add(@Validated @RequestBody DictData dict)
    {
        dict.setCreateBy(getUsername());
        dict.setUpdateBy(getUsername());
        dict.setUpdateTime(new Date());
        dict.setCreateTime(new Date());
        return toAjax(dictDataService.insert(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/dict/data/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        this.dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
